﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMSalesManagement.Manage.StaffManage
{
    public partial class MainStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMembers();
                Session["gvId"] = null;
            }
        }
        private void BindMembers()
        {
            try
            {
                int pageNum = 1;
                if (Request.QueryString["page"] != null)
                {
                    pageNum = int.Parse(Request.QueryString["page"]);
                }
                int total = 0;
                var result = CRM.Business.Staff.PageListStaffs(out total, pageNum, this.DataPage1.ShowRecordCount);
                this.DataPage1.TotalRecordCount = total;
                this.DataPage1.ShowPageNumberCount = pageNum;
                if (result != null)
                {
                    this.gvStaff.DataSource = result;
                    this.gvStaff.DataBind();
                }
                #region 控制分页控件
                CRM.Business.ShowPageDispaly.PageDispaly(this.DataPage1, pageNum, total);
                #endregion
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('数据加载失败！');</script>");
            }
        }

        protected void gvStaff_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Session["gvId"] = e.NewSelectedIndex;//记录选中行的索引
            ShowInfor();
            //存储已选中行的id
            ViewState["chosen"] = Convert.ToInt32(gvStaff.DataKeys[Convert.ToInt32(Session["gvId"])].Value);
        }
        private void ShowInfor()
        {
            if (Convert.ToInt32(Session["gvId"]) <= this.gvStaff.Rows.Count - 1)
            {
                var members =CRM.Business.Staff.GetMemberById(Convert.ToInt32(gvStaff.DataKeys[Convert.ToInt32(Session["gvId"])].Value));
                if (members != null)
                {
                    this.lblEmployeeID.Text = members.employeeID.ToString();
                    this.lblDeparment.Text = members.department;
                    this.lblPosition.Text = members.position;
                    this.lblName.Text = members.name;
                    this.lblPosition.Text = members.position;
                    this.lblSex.Text = members.sex;
                    this.lblAge.Text = members.age.ToString();
                    this.lblBirthday.Text = members.birthday.ToString();
                    this.lblState.Text = members.state == true ? "在职" : "离职";
                    this.lblEnter.Text = members.enterTime.ToString();
                    this.lblAddress.Text = members.address;
                    this.lblMobile.Text = members.mobilePhone;
                    this.lblHomephone.Text = members.homePhone;
                    this.lblUpdate.Text = members.updateInfor.ToString();
                    this.lblRemark.Text = members.remark;
                }
            }
            else//当连续删除最后一行时
            {
                this.lblEmployeeID.Text ="";
                this.lblDeparment.Text = "";
                this.lblPosition.Text = "";
                this.lblName.Text = "";
                this.lblPosition.Text = "";
                this.lblSex.Text = "";
                this.lblAge.Text = "";
                this.lblBirthday.Text = "";
                this.lblState.Text = "";
                this.lblEnter.Text = "";
                this.lblAddress.Text = "";
                this.lblMobile.Text = "";
                this.lblHomephone.Text = "";
                this.lblUpdate.Text = "";
                this.lblRemark.Text = "";
            }
        }
        //为选中行创建一个选择按钮的单击事件
        protected void gvStaff_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {    //获取行索引
                int index = e.Row.RowIndex;
                //根据行索引获取数据库主键id的值
                string pk = this.gvStaff.DataKeys[index].Value.ToString();
                //给每一行tr赋值。
                e.Row.Attributes["id"] = "tr" + pk;
                var btn = (Button)e.Row.FindControl("Btn_Select");
                if (btn != null)
                {
                    //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
                    e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#E3F7FF',this.style.fontWeight='';");
                    //当鼠标离开的时候 将背景颜色还原的以前的颜色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
                    e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#65B8FF';");
                    //e.Row.Cells[0].Attributes["style"] = "this.style.backGround:url(/Image/Index/selected.png)";
                    e.Row.Attributes["style"] = "Cursor:pointer"; //鼠标悬停的行显示正常
                    e.Row.Attributes["onclick"] = String.Format("javascript:document.getElementById('{0}').click()", btn.ClientID);

                }
            }
        }
    }
}